package com.lzs.adx.web.modules.job.support.faker;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzs.adx.commmon.utils.DateUtils;
import com.lzs.adx.web.commmon.utils.BigDecimalUtil;
import com.lzs.adx.web.modules.adx.dto.AccountDTO;
import com.lzs.adx.web.modules.adx.dto.FakerExtend;
import com.lzs.adx.web.modules.adx.dto.ReportDTO;
import com.lzs.adx.web.modules.adx.dto.ReportTotalDTO;
import com.lzs.adx.web.modules.adx.entity.ReportEntity;
import com.lzs.adx.web.modules.adx.service.AccountService;
import com.lzs.adx.web.modules.adx.service.BackDataService;
import com.lzs.adx.web.modules.adx.service.ReportService;
import com.lzs.adx.web.modules.adx.service.ReportTotalService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 按比例回调的策略
 */
@Component
public class PerFakerBackStrategy extends AbstractFakerBackStrategy{

    @Autowired
    private AccountService accountService;

    @Autowired
    private ReportService reportService;

    @Autowired
    private ReportTotalService reportTotalService;

    // 创建一个固定大小为5的线程池
    ExecutorService executorService = Executors.newFixedThreadPool(5);

    @Override
    public void handerFakerBack(AccountDTO accountDTO, FakerExtend fakerExtend) {
        String id = accountDTO.getId();

        Map<String, String> clickConfigMap = fakerExtend.getClickConfigMap();
        String act = clickConfigMap.get("act");

        Map<String,Object> params=new  HashMap();
        params.put("dspId",accountDTO.getId());
        params.put("date", DateUtils.getCurrentTime());
        List<ReportTotalDTO> list = reportTotalService.list(params);

        if(StringUtils.isNotEmpty(act) && CollectionUtil.isNotEmpty(list)) {
            BigDecimal decimalAct = new BigDecimal(act);
            executorService.execute(() -> {
                if(CollectionUtil.isEmpty(list)){return;}
                ReportTotalDTO reportTotalDTO = list.get(0);
                long cpcTotal = Objects.isNull(reportTotalDTO.getCpcTotal()) ? 0 : reportTotalDTO.getCpcTotal();
                long needValue = decimalAct.multiply(new BigDecimal(cpcTotal)).longValue();
                Long actValue = Objects.isNull(reportTotalDTO.getAct())?0:reportTotalDTO.getAct();
                if(actValue.compareTo(needValue)==-1){
                    Long  needCallBackNumber= needValue-actValue;
                    List<ReportEntity> reportEntities = reportService.selectListByMap(accountDTO.getId(), DateUtils.getCurrentTime(), needCallBackNumber.intValue());
                   for (ReportEntity reportEntity : reportEntities){
                       String format = String.format(reportEntity.getCallback(), reportEntity.getBizRequestId());
                       JSONObject jsonObject = JSON.parseObject(accountDTO.getDspExtend());
                       String eventType = jsonObject.getString("eventType");
                   }
                }

            });
        }


        String register = clickConfigMap.get("register");
        String retain = clickConfigMap.get("retain");
        String pay = clickConfigMap.get("pay");
        String wakeup = clickConfigMap.get("wakeup");



    }
}
